<template>
	<view>
		<myhead left="1"></myhead>
		<view class="tit">发布普通广告</view>
		<view class="inp" @tap="xzBtn">
			<input type="text" :value="stateTxt" />
			<view>选择</view>
		</view>
		<view class="inp" style="margin-bottom: 6vw;">
			<input type="text" value="USDT" disabled/>
		</view>
		<!-- <view class="sxf">手续费：5%</view> -->
		<view class="inputList">
		    <view class="title">固定价格<span>单位：CNY</span></view>
		    <input placeholder="请输入固定价格" v-model="gdj"></input>
		</view>
		<view class="inputList">
		    <view class="title">交易数量</view>
			<span class="tbNum">
				<input placeholder="请输入交易数量" v-model="num"></input>
				<view @tap='qbBtn' class="qbBtn">最大</view>
			</span>
		</view>
		<view class="inputList">
		    <view class="title">最小单笔限额</view>
		    <input placeholder="请输入最小单笔限额" v-model="minNum"></input>
		</view>
		<view class="inputList">
		    <view class="title">最大单笔限额</view>
			<span class="tbNum">
				<input placeholder="请输入最大单笔限额" v-model="maxNum"></input>
				<view @tap='qbBtn1' class="qbBtn">最大</view>
			</span>
		</view>
		<view class="inputList">
		    <view class="title">交易银行卡</view>
		    <input placeholder="点击选择交易银行卡" @tap="cardBtn" disabled v-model="card"></input>
		</view>
		<view class="inputList">
		    <view class="title">付款期限<span>单位：分钟</span></view>
		    <input placeholder="请输入付款期限" v-model="time"></input>
		</view>
		<view class="inputList">
		    <view class="title">交易备注</view>
		    <textarea placeholder="请输入交易备注" v-model="txt"></textarea>
		</view>
		<view class="inputList">
		    <view class="title">资金密码</view>
		    <input placeholder="请输入资金密码" password v-model="zjmm"></input>
		</view>
		<view class="btn" @tap="btn">确定</view>
	</view>
</template>

<script>
	const http = require('../../utils/http.js');
	export default {
		data() {
			return {
				stateTxt:'出售',
				state:0,
				gdj:null,
				minNum:null,
				maxNum:null,
				num:null,
				card:null,
				cardId:null,
				cardListTxt:[],
				time:null,
				txt:null,
				zjmm:null,
				id:null,
				list:null
			}
		},
		onLoad(options) {
			if(options.id){
				uni.showLoading({
				    title: '加载中'
				});
				this.id=options.id
				http.get('/api/purchase/getMyProduct',{productId:options.id}).then(res => {
					//console.log(res)
					if (res.code == 200) {
						if(res.result.buyType==0){
							this.stateTxt='出售'
						}else{
							this.stateTxt='购买'
						}
						this.state=res.result.buyType
						this.gdj=res.result.amount
						this.minNum=res.result.minAmount
						this.maxNum=res.result.maxAmount
						this.num=res.result.pnumber
						this.time=res.result.payMinute
						this.txt=res.result.remark
						this.cardId=res.result.bankId
					}
					uni.hideLoading();
				})
			}
		},
		onShow() {
			uni.showLoading({
			    title: '加载中'
			});
			http.get('/api/user/accountInfo').then(res => {
				if (res.code == 200 && res.result) {
					this.list=res.result
					http.get('/api/user/getMyBank').then(res1 => {
						if (res1.code == 200 && res1.result) {
							this.cardList=res1.result
							for(var i=0;i<res1.result.length;i++){
								this.cardListTxt[i]=res1.result[i].bankName
								if(this.cardId==res1.result[i].id){
									this.card=res1.result[i].bankName
								}
							}
						}
					})
				}
				uni.hideLoading();
			})
		},
		methods: {
			cardBtn:function(){
				let that=this
				//console.log(that.cardListTxt)
				var cardList=that.cardList
				if(that.cardList.length){
					uni.showActionSheet({
					    itemList: that.cardListTxt,
					    success: function (res) {
					        //console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
							that.card=that.cardListTxt[res.tapIndex]
							that.cardId=cardList[res.tapIndex].id
							//console.log(that.cardId)
					    },
					    fail: function (res) {
					        console.log(res.errMsg);
					    }
					});
				}else{
					uni.showToast({
					  title: '请到安全中心添加银行卡',
					  icon: 'none'
					});
				}
			},
			qbBtn:function(){
				this.num=this.list.usdtAmount
			},
			qbBtn1:function(){
				if(this.gdj && this.num){
					this.maxNum=(this.gdj*this.num).toFixed(2)
				}else{
					uni.showToast({
					  title: '先输入固定价和交易数量',
					  icon: 'none'
					});
				}
			},
			xzBtn:function(){
				let that=this
				uni.showActionSheet({
				    itemList: ['出售', '购买'],
				    success: function (res) {
				        //console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
						that.state=res.tapIndex
						if(res.tapIndex==0){
							that.stateTxt='出售'
						}else{
							that.stateTxt='购买'
						}
				    },
				    fail: function (res) {
				        console.log(res.errMsg);
				    }
				});
			},
			btn:function(){
				if(!this.gdj){
					uni.showToast({
					  title: '请输入固定价格',
					  icon: 'none'
					});
					return false
				}
				if(!this.minNum){
					uni.showToast({
					  title: '请输入最小单笔限额',
					  icon: 'none'
					});
					return false
				}
				if(!this.maxNum){
					uni.showToast({
					  title: '请输入最大单笔限额',
					  icon: 'none'
					});
					return false
				}
				if(!this.cardId){
					uni.showToast({
					  title: '点击选择交易银行卡,如果没有请先去--安全中心--银行卡',
					  icon: 'none'
					});
					return false
				}
				if(!this.num){
					uni.showToast({
					  title: '请输入固定价格',
					  icon: 'none'
					});
					return false
				}
				if(!this.time){
					uni.showToast({
					  title: '请输入付款期限',
					  icon: 'none'
					});
					return false
				}
				if(!this.txt){
					uni.showToast({
					  title: '请输入交易备注',
					  icon: 'none'
					});
					return false
				}
				if(!this.zjmm){
					uni.showToast({
					  title: '请输入资金密码',
					  icon: 'none'
					});
					return false
				}
				let id=''
				if(this.id){
					id=this.id
				}
				http.post('/api/purchase/addMyProduct',{id:id,buyType:this.state,amount:this.gdj,minAmount:this.minNum,maxAmount:this.maxNum,bankId:this.cardId,pNumber:this.num,payMinute:this.time,remark:this.txt,security:this.zjmm}).then(res => {
					//console.log(res)
					uni.showToast({
					  title: res.message,
					  icon: 'none',
					  duration:1000
					});
					if (res.code == 200) {
						setTimeout(function () {
						    uni.navigateBack({
						        delta: 1
						    });
						}, 1000);
					}
				})
			}
		}
	}
</script>

<style>
	page{
		padding-bottom: 10vw;
	}
	.tit{
		padding: 4vw;
		font-size: 7vw;
		color: #4CD964;
	}
	.inp{
		width: 84%;
		padding: 2vw 4vw;
		border-radius: 1vw;
		background: #f9f9f9;
		font-size: 3.8vw;
		color: #333333;
		margin: 0 auto;
		border: 1px solid #f2f2f2;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		margin-top: 4vw;
	}
	.sxf{
		padding: 2vw 4vw;
		margin: 4vw 0;
		color: #007AFF;
	}
	.inputList{
		padding: 0 4vw;
		margin-bottom: 6vw;
	}
	.inputList .title{
		margin-bottom: 2vw;
		font-size: 3.6vw;
		color: #999999;
	}
	.inputList input{
		border-radius: 1vw;
		background: #f9f9f9;
		font-size: 3.8vw;
		height: 10vw;
		line-height: 10vw;
		text-indent: 2vw;
	}
	.title span{
		color: #007AFF;
		margin-left: 4vw;
	}
	textarea{
		width: 96%;
		font-size: 3.8vw;
		border: 1px solid #f2f2f2;
		border-radius: 2vw;
		padding: 2%;
	}
	.btn{
		width: 84%;
		padding: 4vw 0;
		font-size: 3.5vw;
		border-radius: 1.8vw;
		background-image: linear-gradient(45deg, #24EBA9, #10C7B2);
		color: #FFFFFF;
		text-align: center;
		margin: 0 auto;
		margin-top: 10vw;
	}
	.tbNum{
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.tbNum input{
		width: 80%;
	}
	.qbBtn{
		width: 20%;
		text-align: center;
		color: #24EBA9;
		font-size: 3.8vw;
	}
</style>
